//
//  XCAdvertisementVC.m
//  XCNewsApp
//
//  Created by lyx on 16/12/8.
//  Copyright © 2016年 XC. All rights reserved.
//

#import "XCAdvertisementVC.h"
#import "XCAdvertiseModel.h"
#import "UIImageView+WebCache.h"
#import "XCTabBarController.h"
#define iPhone6P  screenH == 736
#define iPhone6   screenH == 667
#define iPhone5   screenH == 568
#define iPhone4   screenH == 480
#define code @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
@interface XCAdvertisementVC ()

/**背景*/
@property (weak, nonatomic) IBOutlet UIImageView *backImage;
/**广告的View*/
@property (weak, nonatomic) IBOutlet UIView *ADView;
/**跳过广告的按钮*/
@property (weak, nonatomic) IBOutlet UIButton *passBtn;
/**模型*/
@property (strong, nonatomic) XCAdvertiseModel *model;
/**定时器*/
@property (weak, nonatomic) NSTimer *timer;
/**AFN的管理*/
@property (strong, nonatomic) AFHTTPSessionManager *manager;
@end

@implementation XCAdvertisementVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBackImageView];
    [self dataload];
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeGo) userInfo:nil repeats:YES];
    self.timer = timer;
    [timer fire];
    
}

/**
 设置背景图片
 */
- (void)setBackImageView{
    if (iPhone6P) {
        self.backImage.image = [UIImage imageNamed:@"LaunchImage-700"];
    }else if (iPhone6){
        self.backImage.image = [UIImage imageNamed:@"LaunchImage-700"];
    }else if (iPhone5){
        self.backImage.image = [UIImage imageNamed:@"LaunchImage-700"];
    }else if (iPhone4){
        self.backImage.image = [UIImage imageNamed:@"LaunchImage-700"];
    }
}


- (void)dataload{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    self.manager = manager;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    [manager GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:@{@"code2" : code} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
        NSArray* adArr = responseObject[@"ad"];
        if (adArr.count==0) {
            [self passBtnClick:nil];
            return;
        };
        NSDictionary* adDic = adArr.firstObject;
        XCAdvertiseModel *model = [XCAdvertiseModel mj_objectWithKeyValues:adDic];
        self.model = model;
        CGFloat h = model.h * screenW / model.w;
        CGRect frame = CGRectMake(0, 0, screenW, h);
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:frame];
        imageV.userInteractionEnabled = YES;
        [self.ADView addSubview:imageV];
        [imageV sd_setImageWithURL:[NSURL URLWithString:model.w_picurl]];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [imageV addGestureRecognizer:tap];
    } failure:^( NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)timeGo{
    static int sec = 3;
    sec--;
    if (sec == -1) {
        [self passBtnClick:nil];
    }
}

- (IBAction)passBtnClick:(UIButton *)sender {
    [self.timer invalidate];
    XCTabBarController *TBC = [[XCTabBarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController = TBC;
}

- (void)tap{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:self.model.ori_curl]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.model.ori_curl]];
    }
}

//- (void)viewWillDisappear:(BOOL)animated{
//    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
//    [super viewWillDisappear:animated];
//}
@end
